import {
  login,
  logout,
  getUserInfo,
  getAllAuth
} from '@/api/user'
import {
  setToken,
  setHasLogin,
  getHasLogin,
  getUserId,
  setUserId,
  getAccess,
  setAccess,
  setUserName
} from '@/libs/util'
import {
  base64Encoder
} from "@/libs/base64Encoder";
import router from '@/router'
export default {
  state: {
    userName: '',
    userId: getUserId(),
    access: JSON.parse(getAccess()),
    hasGetInfo: false,
    hasLogin: getHasLogin(),
  },
  mutations: {
    setAvatar(state, avatarPath) {
      state.avatarImgPath = avatarPath
    },
    setUserId(state, id) {
      state.userId = id
      setUserId(id);
    },
    setHasLogin(state, status) {
      state.hasLogin = status;
      setHasLogin(status);
    },
    setUserName(state, name) {
      state.userName = name
      setUserName(name);
    },
    setAccess(state, access) {
      state.access = access;
      setAccess(access);
    },
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    setHasGetInfo(state, status) {
      state.hasGetInfo = status
    },
  },
  getters: {

  },
  actions: {
    // 登录
    handleLogin({
      dispatch,
      commit
    }, {
      phone,
      code
    }) {
      phone = phone.trim();
      code = base64Encoder(code);
      return new Promise((resolve, reject) => {
        login({
          phone,
          code
        }).then(res => {
          const data = res.data;
          if (res.data.code == 200 || res.data.code == 201) {
            commit('setToken', data.data.yjyToken)
            commit('setUserId', data.data.yjyUserId)
            commit('setUserName', data.data.userName);
            commit('setHasLogin', true);
            resolve(data);
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    /* // 获取用户相关信息
    getUserInfo ({ state, commit }) {
      return new Promise((resolve, reject) => {
        try {
          getUserInfo().then(res => {
            const data = res.data
            commit('setAvatar', data.avatar)
            commit('setUserName', data.name)
            commit('setUserId', data.user_id)
            commit('setAccess', data.access)
            commit('setHasGetInfo', true)
            resolve(data)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })
    }, */

    // 获取权限
    getAuth({
      commit
    }) {
      return new Promise((resolve, reject) => {
        getAllAuth(2).then(res => {
          var authArr = res.data.data;
          commit('setAccess', authArr);
          resolve()
        })
      })
    },
    // 退出登录
    handleLogOut({
      state,
      commit
    }) {
      return new Promise((resolve, reject) => {
        logout().then((res) => {
          commit('setUserId', '');
          commit('setAccess', '');
          commit('setToken', '');
          commit('setUserName', '');
          commit('setHasLogin', false);
          resolve()
        }).catch(err => {
          reject(err)
        })
      })
    },
  }
}